/**
 * @(#)DataMigrationTool.java, 2010-5-16. 
 * 
 * Copyright 2010 Youdao, Inc. All rights reserved.
 * YOUDAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.rsstuan.experiment.tool;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.rsstuan.storage.data.TuanGouChannelData;
import com.rsstuan.storage.data.TuanGouSiteData;
import com.rsstuan.storage.impl.JDOTuanGouChannelStorage;
import com.rsstuan.storage.impl.JDOTuanGouSiteStorage;
import com.rsstuan.web.controller.SpringController;

/**
 *
 * @author guosq
 *
 */
public class DataMigrationTool extends SpringController{

	private static final Logger LOG = Logger.getLogger(DataMigrationTool.class.getName());
    
    public void migrateSite() throws IOException {
        
        JDOTuanGouSiteStorage storage = new JDOTuanGouSiteStorage();
        List<TuanGouSiteData> sites = storage.getAllSites();
        for (TuanGouSiteData site : sites ) {
           try {
        	   migrateOneSite(site);
           }
           catch (Exception e) {
        	   LOG.log(Level.WARNING, "migrateOne site=" + site.getSiteName() + " failed", e);
           }
        }
    }
    
    
    public void migrateChannels() {
    	JDOTuanGouChannelStorage storage = new JDOTuanGouChannelStorage();
    	List<TuanGouChannelData> channels = storage.getAllChannels();
    	for (TuanGouChannelData channel : channels) {
    		try {
    			migrateOneChannel(channel);
    		}
    		catch (Exception e) {
    			LOG.log(Level.WARNING, "migrateOne channel=" + channel.getTitle() + " failed", e);
    		}
    	}
    }
    private void migrateOneSite(TuanGouSiteData site) throws IOException {
    	String toAddress = "http://tuansifang.appspot.com/add.do?method=addSite";
        URL url = new URL(toAddress);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8"); 
       
        StringBuilder sb = new StringBuilder();
        sb.append("submit=true");
        sb.append("&siteId="+ site.getId());
        sb.append("&siteName="+site.getSiteName());
        sb.append("&link="+ site.getLink());
        sb.append("&description=" + site.getDescription()); 
        
        out.write(sb.toString());
        out.flush();
        out.close();
        
        InputStreamReader input = new InputStreamReader(connection.getInputStream(), "utf-8");
    }
    
    private void migrateOneChannel(TuanGouChannelData channel) throws IOException {
    	String toAddress = "http://tuansifang.appspot.com/add.do?method=addChannel";
        URL url = new URL(toAddress);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8"); 
       
        StringBuilder sb = new StringBuilder();
        sb.append("submit=true");
        sb.append("&channelId=" + channel.getId());
        sb.append("&title=" + channel.getTitle());
        sb.append("&siteId="+ channel.getSiteId());
        sb.append("&city="+channel.getCity());
        sb.append("&link="+ channel.getLink());
        sb.append("&rss="+ channel.getRss());
        sb.append("&description=" + channel.getDescription()); 
        
        out.write(sb.toString());
        out.flush();
        out.close();
        
        InputStreamReader input = new InputStreamReader(connection.getInputStream(), "utf-8");
    }
}
